package cn.mw.cmdb.entity;

import cn.mw.cmdb.param.ImportContextParam;
import cn.mw.components.mongodb.entity.CriteriaOpsType;
import cn.mw.microMonitorCommon.entity.cmdb.ModelExportDataInfoParam;
import cn.mw.microMonitorCommon.entity.cmdb.MwImportResultParam;
import com.google.common.base.Strings;
import org.springframework.data.mongodb.core.query.Criteria;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;

public class PropertyTypeMoney extends PropertyType<PropertyTypeMoney, Object> {
    public static final String ID = "money";
    public static final String VALUE_KEY = "value";
    public static final String UNIT_KEY = "unit";
    public static final String DEFAULT_UNIT = "元";


    //判断数字(包含负数和小数)
    public static final String numericPattern = "^[-+]?\\d*\\.?\\d+$";

    public PropertyTypeMoney() {
        setId(ID);
        setName("金额");
        setValueTip("请输入数字");
        setSortNum(22);
    }

    public boolean validateValue(Object value, PropertyTypeRef propertyTypeRef) throws Exception {
        if (value instanceof String) {
            new BigDecimal(value.toString());
        }
        return true;
    }

    //    @Override
//    public Object convertValue(Object value) {
//        if(value !=null && isNumeric(strValueConvert(value))){
//            //格式转换
//            return formDecimal(strValueConvert(value));
//        }
//        return value;
//    }
//
    @Override
    public Object readValue(Object val, Map<String, Object> context) {
        String str = "";
        Map m = new HashMap();
        if (val != null && val instanceof Map) {
            m = (Map) (val);
//            str =  "".equals(strValueConvert(m.get(VALUE_KEY)))?"0":strValueConvert(m.get(VALUE_KEY));
//            String unit = strValueConvert(m.get(UNIT_KEY));
        }
        return m;
    }

    @Override
    public Object exportValue(Object val, Map<String, Object> context) {
        Object obj = readValue(val, context);
        String str = "";
        Map m = new HashMap();
        if (obj != null && obj instanceof Map) {
            m = (Map) (val);
            str = "".equals(strValueConvert(m.get(VALUE_KEY))) ? "0" : strValueConvert(m.get(VALUE_KEY));
            String unit = strValueConvert(m.get(UNIT_KEY));
            return str + unit;
        }
        return str;
    }


    @Override
    public Criteria doCriteria(CriteriaOpsType opsType, String name, Object value) {
        Criteria criteria = null;
        switch (opsType) {
            case is:
                criteria = new Criteria(name).is(value);
                break;
            case gt:
                criteria = new Criteria(name).gt(value);
                break;
            case gte:
                criteria = new Criteria(name).gte(value);
                break;
            case lt:
                criteria = new Criteria(name).lt(value);
                break;
            case lte:
                criteria = new Criteria(name).lte(value);
                break;
            case exist:
                criteria = new Criteria(name).exists((boolean) value);
                break;
            case ne:
                criteria = new Criteria(name).ne(value);
        }
        return criteria;
    }

    @Override
    public CriteriaOpsType getDefaultCriteriaOpsType() {
        return CriteriaOpsType.is;
    }

    @Override
    public PropertyTypeMoney newObj() {
        return new PropertyTypeMoney();
    }

    @Override
    public Object getDataValue(ImportContextParam contextParam) {
        MwImportResultParam resultParam = new MwImportResultParam();
        String message = "";
        ModelExportDataInfoParam importParam = contextParam.getParam();
        //属性名称
        String tableName = importParam.getTableName();
        Object cellValue = importParam.getCellValue();
        Map<String, Object> matchMap = new HashMap<>();
        importCheckInfo(resultParam, contextParam, matchMap);
        if (!Strings.isNullOrEmpty(strValueConvert(cellValue)) && !isNumeric(cellValue)) {
            message = tableName + ":金额必须是数字";
        } else {
            Map m = new HashMap();
            String s = formDecimal(strValueConvert(cellValue));
            m.put(VALUE_KEY, s);
            m.put(UNIT_KEY, DEFAULT_UNIT);
            resultParam.setCellValue(m);
        }
        resultParam.setMessage(message);
        return resultParam;
    }

    @Override
    public void refresh() {

    }

    public static boolean isNumeric(Object obj) {
        String str = strValueConvert(obj);
        // 正则表达式匹配数字格式
        String numericPattern = "^[-+]?\\d*\\.?\\d+$";
        return Pattern.matches(numericPattern, str);
    }

    public static String formDecimal(String str) {
        return new BigDecimal(str).setScale(2, BigDecimal.ROUND_HALF_UP).toString();
    }

}
